﻿using Common.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Wallpaper.Models.DTOs.Requests.Admin;
using Wallpaper.Models.DTOs.Responses.Admin;
using Wallpaper.Services.IService;


namespace WallpaperApi.Controllers
{
    [Route("api/admins")]
    [ApiController]
    [Authorize]

    public class AdminsController : BaseController
    {

        readonly IAdminService _adminService;
        readonly ILogger<AdminsController> _logger;
        readonly IConfiguration _configuration;
        public AdminsController(IAdminService adminService,ILogger<AdminsController> logger, IConfiguration configuration)
        {
            this._adminService = adminService;
            this._logger = logger;
            this._configuration = configuration;
            
        }



        /// <summary>
        /// 管理员登录
        /// </summary>
        /// <param name="request">登录请求参数</param>
        /// <returns>包含JWT Token的登录结果</returns>
        /// <remarks>
        /// 示例请求：
        /// POST /api/admin/login
        /// {
        ///     "account": "admin",
        ///     "password": "123456"
        /// }
        /// </remarks>
        /// <response code="200">登录成功</response>
        /// <response code="401">账号或密码错误</response>
        /// <response code="403">账户已被禁用</response>
        /// <response code="500">服务器内部错误</response>
        [HttpPost("login")]
        [ProducesResponseType(typeof(ApiResponse<string>), 200)]
        [ProducesResponseType(typeof(ApiResponse<string>), 401)]
        [ProducesResponseType(typeof(ApiResponse<string>), 403)]
        [ProducesResponseType(typeof(ApiResponse<string>), 500)]
        [AllowAnonymous]
        public async Task<ApiResponse<string>> Login([FromBody] AdminLoginRequest request)
        {
            // 直接调用，让异常被全局异常处理中间件捕获
            var token = await _adminService.LoginAsync(request.Account, request.Password);
            return ApiResponse<string>.Success(token, "登录成功");
        }

        /// <summary>
        /// 获取登录信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("profile")]
        public async Task<ApiResponse<AdminResponse>> Info()
        {
            AdminResponse result =await _adminService.GetInfoAsync(AdminId);
            return ApiResponse<AdminResponse>.Success(result);
        }
 
    }
}
